package com.atguigu.eduservice.controller;


import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.atguigu.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-26
 */
@RestController
@CrossOrigin//跨域
@Api(description = "讲师管理")
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //所有讲师列表
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<EduTeacher> teachers = eduTeacherService.list(null);
        return R.ok().data("items", teachers);
    }

    //逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/deleteTeacher/{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //分页查询讲师列表
    /**
     * @param current 当前页
     * @param limit   每页记录数 条数
     * @return
     */
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name = "current", value = "当前页码", required = true)
                         @PathVariable Long current,
                         @ApiParam(name = "limit", value = "每页记录数", required = true)
                         @PathVariable Long limit) {
        //创建分页对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现
        //调用方法时，提层封装。将分页所有数据封装进pageTeacher对象中
        eduTeacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> record = pageTeacher.getRecords();//数据list集合
        //方式一
        return R.ok().data("total", total).data("record", record);
        //方式二
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",total);
//        map.put("records",records);
//        return R.ok().data(map);
    }


    //带条件的分页查询讲师列表
    @ApiOperation(value = "条件分页讲师列表")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建分页对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //条件都可以为空，下面进行判断。条件不为空，拼接条件
        if (!StringUtils.isEmpty(teacherQuery.getName())) {
            wrapper.like("name", teacherQuery.getName());
        }
        if (!StringUtils.isEmpty(teacherQuery.getLevel())) {
            wrapper.eq("level", teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) {
            wrapper.ge("gmt_create", teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) {
            wrapper.le("gmt_create", teacherQuery.getEnd());
        }
        //按照时间降序排序
        wrapper.orderByDesc("gmt_modified");

        //调用方法
        eduTeacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> record = pageTeacher.getRecords();
        return R.ok().data("total", total).data("rows", record);
    }

    //添加讲师
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true)
                        @RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据讲师id进行查询 为了修改时做数据回显
    @GetMapping("/getTeacher/{id}")
    @ApiOperation(value = "根据讲师id进行查询")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    //讲师修该功能
    @PostMapping("updateTeacher")
    @ApiOperation(value = "讲师修该功能")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

//    @PutMapping("{id}")
//    @ApiOperation(value = "id讲师修该功能")
//    public R updateTeacher1( @RequestBody EduTeacher eduTeacher,@PathVariable String id){
//        eduTeacher.setId("id");
//        boolean flag = eduTeacherService.updateById(eduTeacher);
//        if (flag){
//            return R.ok();
//        }else {
//            return R.error();
//        }
//    }
}

